/**
 *@Description 运单首页
 *@Author zhangnf
 *@Date 2015-09-23
 */
var tsOrderCtrl  = new XFApp.client();
XFApp.client.prototype.order = {
	successCode : "10000",
	page : null,
	checkVal :"",
	urlItems : {
		queryByPageURL : basePath+"/nt/transorder/list",
		delOrderURL : basePath+"/nt/transorder/delTransOrder",
		batchOutAppURL : basePath+"/nt/transorder/batchOutApp",
	},
	queryPage : function(){
		_this = this;
		if(!!_this.page){
			_this.page.page("remote",0,$('#searchForm').serialize());
		}else{
		_this.page = $("#page").page({
			pageSize : 10,
			currentPageIndex : Number($("#currentPageIndex").val())-1,
			remote: {
				url: _this.urlItems.queryByPageURL,
		        params: $('#searchForm').serialize(), 
		        success: function (data, pageIndex) {
		        	$("#currentPageIndex").val(data.result.pageNo);
		        	if(data.code==_this.successCode){
		        		var pageResult = data.result;
		        		if(null == data
								|| null == data.result 
									|| null == data.result.itemList
										|| data.result.itemList.length < 1){
		        			$("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
		        			_this.curOrderItemList = null;
		        		}else{
		        			for(var index in pageResult.itemList){
		        				var element = pageResult.itemList[index];
		        				element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
		        			}
		        			$("#page_grid").html($("#dataTmp").render(pageResult.itemList));
		        			_this.curOrderItemList = pageResult.itemList;
		        		}
		        	}else{
						webDialogUtil.simpleAlert("数据查询失败!");
		        	}
		        },
		        error : function(){
					webDialogUtil.simpleAlert("数据查询失败!");
		        }
			},
			firstBtnText : '首页',
			lastBtnText : '尾页',
			prevBtnText : '上一页',
			nextBtnText : '下一页',
			showInfo : true,
			showJump : true,
			jumpBtnText : '跳转',
			showPageSizes : true,
			pageSizeItems: [20,30,40,50,100,200],
			infoFormat : '共{pageCount}页,{total}项'
		});
		}
	},
	showModal : function(id,type){
		$('#editModal').modal('show');
	},
	delOrderDialog : function(id){
		_this = this;
		webDialogUtil.simpleConfrimAlert("确定要删除此运单记录吗?", function(){tsOrderCtrl.order.delOrder(id);});
	},
	////记录当前页数据，再删除一条数据后局部刷新页面
	curOrderItemList: null,
	//数据删除成功后遍历当前页面的数据，将删除成功的数据从数组中去除，然后重新渲染页面	
	delOrderForPage: function(id){
		for(var i = 0;i < this.curOrderItemList.length;i ++){
			if(this.curOrderItemList[i].id == id){
				this.curOrderItemList.splice(i,1);
			}
		}
		$("#page_grid").html($("#dataTmp").render(this.curOrderItemList));
	},
	delOrder : function(id){
		_this = this;
		$.ajax({
		    url:_this.urlItems.delOrderURL,
		    data:{id:id},  
		    type:'post',  
		    cache:false,  
		    dataType:'json',  
		    success:function(data) {
		    	if(true == data.isSuccess){
					//webDialogUtil.simpleAlert(data.msg);
		    		//tsOrderCtrl.order.queryPage();
		    		_this.delOrderForPage(id);
					window.webDialogUtil.toast({
						type: 'success',
						title: '操作成功'
					});
		    	}else{
		    		window.webDialogUtil.toast({
						title: '操作失败'
					});
		    	}
		     },  
		     error : function() {
				 webDialogUtil.simpleAlert("操作失败!");
		     }
		});
	},
	
	outAppOption : function (type){
		
        _this = this;
        
        if(!_this.checkVal){        	
        	window.webDialogUtil.alert("提示","请选择一条记录进行操作!!");
        	return ;
        }
        
        webAlert({
            title:'提示',
            content:'确认执行'+type+'操作吗?',
            opacity:.5,
            bg:"gray",
            width:300,
            height:10,
            button:[
                    {
                    name:"确定",
                    callback:function(){
                    	_this.outApp();
                    	this.close();
                    }
                    },{
                    name:"取消",
                    callback:function(){
                  	  this.close();
                    }
                    }]
            });
        
        
	},	
	
	outApp: function(){
	    	
	    var index = _this.checkVal.lastIndexOf(",");            
        var id = _this.checkVal.substring(0,index);
        
        var params = { 
        		ids : id 
        		}
	    	
    	$.ajax({
    		url :_this.urlItems.batchOutAppURL,
    		data : params,
    		dataType : "json",
    		type : 'post',
    		success : function(data) {				
    			if(data.isSuccess){
    				webDialogUtil.simpleAlert(data.msg, "提示", null, 450, 10);
    				//移除事件
    				$("#checkAll").removeAttr("checked");
    				$("[name='chkItem']:checkbox").removeAttr("checked");//取消全选
    				tsOrderCtrl.order.queryPage();
    			}else{    				
    				window.webDialogUtil.alert("提示",data.msg);
    			}
    		}
    	});
	},
	
	event : function(){
		_this = this;
		$("#searchButton").on("click",function(){
			tsOrderCtrl.order.queryPage();
		});
		
		//批量离境申报
		$("#outAppBtn").on("click",function(){
			var chkVal = "";
			$("#page_grid input[type='checkbox']").each(function(){
				if($(this).prop("checked")){
					chkVal += $(this).val()+",";
				}
			});		
			_this.checkVal = chkVal ;				
			_this.outAppOption($("#outAppBtn").text());
		});
		
		$("#checkAll").bind("click",function(){				
			var chkVal = ""; 
			//全选
			if($("#checkAll").prop("checked")){
				 $("[name='chkItem']").prop("checked","checked");
				 $("[name='chkItem']:checkbox:checked").each(function (){
					chkVal += $(this).val()+",";
				 })
				   
			 }else{
				 $("[name='chkItem']").removeAttr("checked");//取消全选
				 chkVal = "";
			 }	
			 _this.checkVal = chkVal;
		});
		
		tsOrderCtrl.order.queryPage();
	}
};
$.views.converters({
	ftime : function(value) {
		return  new Date(value*1000).format("yyyy-MM-dd hh:mm:ss");
	},
	fdkstatus : function(value){
		return dksCtrl.dockerstatus.get(value);
	}
});
$(function(){
	dictCtrl.dictionary.load("toDocketStatus","docketStatusHidden");
	tsOrderCtrl.order.event();
});